export type CallBack<T> = (...rest: T[]) => void
export type Event = <T>(type: string, callback: CallBack<T>) => void
export type Emit = <T>(type: string, ...rest: T[]) => void

export default class Events {
  private constructor () {}
  // #region 变量声明
  private static list: Record<string, Set<unknown>> = {}
  // #endregion

  // #region 事件处理函数
  
  // #endregion

  // #region 函数
  // 注册事件
  public static on: Event = (type, callback) => {
    // console.log(this, type)
    if (!this.list[type]) this.list[type] = new Set()
    if (!this.list[type].has(callback)) this.list[type].add(callback)
  }
  // 移除事件
  public static off: Event = (type, callback) => {
    // console.log(type, callback)
    if (!this.list[type]) return
    if (this.list[type].has(callback)) this.list[type].delete(callback)
  }
  // 调用事件
  public static emit: Emit = (type, ...rest) => {
    // console.log(type, rest)
    if (!this.list[type]) return
    this.list[type].forEach((item) => {
      (item as CallBack<typeof rest[0]>)(...rest)
    })
  }
  // #endregion
}
